﻿package core.ui.util
{
    import core.event.*;
    import core.image.encoder.*;
    import core.manager.*;
    import core.ui.controls.*;
    import core.ui.dialogs.*;
    import flash.display.*;
    import flash.events.*;
    import flash.net.*;
    import flash.system.*;
    import flash.utils.*;

    public class SaveImageWindow extends Window
    {
        var bm:Bitmap;
        var stream:ByteArray;
        var removecanvas:Boolean;
        var cancel:Button;
        var ok:Button;
        var message:Literal;
        var size:Literal;
        var overview:Sprite;
        var target:TabBar;
        var cy:int;
        var content:Sprite;
        var iquality:Slidebar;
        var iname:TextInput;
        var iformat:Dropdown;
        var idesc:TextInput;
        var ifolder:Dropdown;
        var overlay:Sprite;
        var connect:OpenImageButton;
        var file:FileReference;
        var loader:URLLoader;

        public function SaveImageWindow(param1:Tuyitu, param2:Boolean = false)
        {
            this.removecanvas = param2;
            super(param1, U.RM("rm_save_image"), false);
            closeme.addEventListener(MouseEvent.MOUSE_UP, this.Close, false, 0, true);
            this.Resize(560, 350);
            this.AddToStage(param2);
            this.target = new TabBar();
            this.target.addEventListener(SelectEvent.COMMAND, this.TargetSelect, false, 1, true);
            if (U.IsParam("target"))
            {
                this.target.AddTab("referrer", new ReferrerIcon(), U.IsParam("referrer") ? (U.Param("referrer")) : ("rm_referrer"));
            }
            if (!U.IsParamTrue("locktarget") || !U.IsParam("target"))
            {
                this.target.AddTab("computer", new ComputerIcon(), "rm_my_computer");
            }
            if (!U.IsParamTrue("locktarget"))
            {
                this.target.AddTab("m5cn", new M5cnIcon(), "定制我的礼物");
                this.target.AddTab("tuyitu", new TUYITUIcon(), "我的素材库");
                this.target.AddTab("qqapi", new QQapiIcon(), "QQ相册");
            }
            this.target.x = 4;
            this.target.y = 22;
            this.target.Resize(this.width - 8);
            addChild(this.target);
            this.overview = new Sprite();
            this.overview.x = 20;
            this.overview.y = 70;
            addChild(this.overview);
            this.bm = new Bitmap(null, "auto", true);
            this.bm.x = 2;
            this.bm.y = 2;
            this.overview.addChild(this.bm);
            this.DrawThumb();
            this.content = new Sprite();
            this.content.x = this.overview.x + this.overview.width + 15;
            this.content.y = 65;
            this.message = new Literal("rm_working", true);
            this.message.x = this.width - 220;
            this.message.y = this.height - 30;
            this.message.visible = false;
            this.message.width = 200;
            addChild(this.message);
            this.size = new Literal("", true);
            this.size.x = this.width - 220;
            this.size.y = this.height - 60;
            this.size.width = 200;
            addChild(this.size);
            this.ok = new Button("rm_ok", 70);
            this.ok.addEventListener(MouseEvent.CLICK, this.Save, false, 0, true);
            this.ok.x = this.width - 82;
            this.ok.y = this.height - 33;
            addChild(this.ok);
            this.cancel = new Button("rm_cancel", 70);
            this.cancel.addEventListener(MouseEvent.CLICK, this.Close, false, 0, true);
            this.cancel.x = this.width - 162;
            this.cancel.y = this.height - 33;
            addChild(this.cancel);
            addChild(this.content);
            this.target.Select(U.IsParam("target") ? ("referrer") : (AppState.LastSaveTab), true);
            return;
        }// end function

        private function TargetSelect(... args) : void
        {
            args = new activation;
            var _args:* = args;
            var type:*;
            var ltn:*;
            var ltf:*;
            var args:* = ;
            this.HideConnect();
            if (this.target.value != "computer" && this.target.value != "referrer" && this.target.value != "m5cn" && ed.user == null)
            {
                new LoginDialog(ed, this.TargetSelect, function ()
            {
                target.Select(AppState.LastSaveTab, true);
                return;
            }// end function
            );
            }
            else
            {
                this.cy = 0;
                AppState.LastSaveTab = this.target.value;
                type = ed.canvas.document.GetType();
                while (this.content.numChildren > 0)
                {
                    
                    this.content.removeChildAt(0);
                }
                switch(this.target.value)
                {
                    case "computer":
                    {
                        ltn = new Literal("rm_name");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iname = new TextInput(250, 100, U.CleanString(ed.canvas.document.GetName()));
                        this.iname.t.restrict = "^,\\/&?<>(){}[]#@";
                        this.iname.y = this.cy;
                        this.content.addChild(this.iname);
                        this.cy = this.cy + 35;
                        ltf = new Literal("rm_format");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iformat = new Dropdown(250, 250);
                        this.iformat.addEventListener(SelectEvent.COMMAND, this.FormatChange, false, 1, true);
                        this.iformat.AddItem(U.RM("rm_jpeg"), "jpg");
                        this.iformat.AddItem(U.RM("rm_png"), "png");
                        this.iformat.AddItem(U.RM("rm_bmp"), "bmp");
                        this.iformat.AddItem(U.RM("rm_tud"), "tud");
                        this.iformat.Bind();
                        this.iformat.y = this.cy;
                        if ( == "gif")
                        {
                            type;
                        }
                        else if ( != "jpg" &&  != "png" &&  != "bmp" &&  != "tud")
                        {
                            type;
                        }
                        this.iformat.Set();
                        this.cy = this.cy + 35;
                        this.iquality = new Slidebar("rm_quality", 0, 100, 250, 80, false);
                        this.iquality.addEventListener(MouseEvent.MOUSE_DOWN, this.InitEncode, false, 1, true);
                        this.iquality.y = this.cy;
                        this.content.addChild(this.iquality);
                        this.content.addChild(this.iformat);
                        setTimeout(this.FormatChange, 500);
                        break;
                    }
                    case "referrer":
                    {
                        ltn = new Literal("rm_name");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iname = new TextInput(250, 100, U.CleanString(ed.canvas.document.GetName()));
                        this.iname.t.restrict = "^,\\/&?<>(){}[]#@";
                        this.iname.Disable(U.IsParamTrue("locktitle"));
                        this.iname.y = this.cy;
                        this.content.addChild(this.iname);
                        this.cy = this.cy + 35;
                        ltf = new Literal("rm_format");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iformat = new Dropdown(250, 250);
                        this.iformat.addEventListener(SelectEvent.COMMAND, this.FormatChange, false, 1, true);
                        this.iformat.AddItem(U.RM("rm_jpeg"), "jpg");
                        this.iformat.AddItem(U.RM("rm_png"), "png");
                        this.iformat.Bind();
                        this.iformat.y = this.cy;
                        if (U.IsParam("locktype"))
                        {
                            if (U.Param("locktype", true) != "source")
                            {
                                type = U.Param("locktype", true);
                            }
                            this.iformat.Disable(true);
                        }
                        if ( == "gif")
                        {
                            type;
                        }
                        else if ( != "jpg" &&  != "png")
                        {
                            type;
                        }
                        this.iformat.Set();
                        this.cy = this.cy + 35;
                        this.iquality = new Slidebar("rm_quality", 0, 100, 250, 80, false);
                        this.iquality.addEventListener(MouseEvent.MOUSE_DOWN, this.InitEncode, false, 1, true);
                        this.iquality.y = this.cy;
                        this.content.addChild(this.iquality);
                        if (U.IsParam("lockquality"))
                        {
                            this.iquality.Set(Number(U.Param("lockquality")));
                            this.iquality.Disable();
                        }
                        this.content.addChild(this.iformat);
                        setTimeout(this.FormatChange, 500);
                        break;
                    }
                    case "m5cn":
                    {
                        ltn = new Literal("rm_description");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.idesc = new TextInput(250, 300, U.CleanString(ed.canvas.document.GetName()), 50);
                        this.idesc.t.wordWrap = true;
                        this.idesc.y = this.cy;
                        this.content.addChild(this.idesc);
                        this.cy = this.cy + 65;
                        ltf = new Literal("rm_format");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iformat = new Dropdown(250, 250);
                        this.iformat.addEventListener(SelectEvent.COMMAND, this.FormatChange, false, 1, true);
                        this.iformat.AddItem(U.RM("rm_jpeg"), "jpg", true);
                        this.iformat.AddItem(U.RM("rm_png"), "png");
                        this.iformat.Bind();
                        this.iformat.y = this.cy;
                        if ( == "gif")
                        {
                            type;
                        }
                        else if ( != "jpg" &&  != "png")
                        {
                            type;
                        }
                        this.iformat.Set();
                        this.cy = this.cy + 35;
                        this.iquality = new Slidebar("rm_quality", 0, 100, 250, 80, false);
                        this.iquality.addEventListener(MouseEvent.MOUSE_DOWN, this.InitEncode, false, 1, true);
                        this.iquality.y = this.cy;
                        this.content.addChild(this.iquality);
                        this.content.addChild(this.iformat);
                        setTimeout(this.FormatChange, 500);
                        break;
                    }
                    case "tuyitu":
                    {
                        ltn = new Literal("rm_name");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iname = new TextInput(250, 100, U.CleanString(ed.canvas.document.GetName()));
                        this.iname.t.restrict = "^,\\/&?<>(){}[]#@";
                        this.iname.y = this.cy;
                        this.content.addChild(this.iname);
                        this.cy = this.cy + 35;
                        ltf = new Literal("rm_folder");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.ifolder = new Dropdown(250, 250);
                        this.ifolder.AddItem(U.RM("rm_working"), "", true);
                        this.ifolder.Bind();
                        this.ifolder.y = this.cy;
                        this.ifolder.Disable();
                        this.cy = this.cy + 35;
                        ltf = new Literal("rm_format");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iformat = new Dropdown(250, 250);
                        this.iformat.addEventListener(SelectEvent.COMMAND, this.FormatChange, false, 1, true);
                        this.iformat.AddItem(U.RM("rm_jpeg"), "jpg");
                        this.iformat.AddItem(U.RM("rm_png"), "png");
                        this.iformat.AddItem(U.RM("rm_bmp"), "bmp");
                        this.iformat.AddItem(U.RM("rm_tud"), "tud");
                        this.iformat.Bind();
                        this.iformat.y = this.cy;
                        if ( == "gif")
                        {
                            type;
                        }
                        else if ( != "jpg" &&  != "png" &&  != "bmp" &&  != "tud")
                        {
                            type;
                        }
                        this.iformat.Set();
                        this.cy = this.cy + 35;
                        this.iquality = new Slidebar("rm_quality", 0, 100, 250, 80, false);
                        this.iquality.addEventListener(SliderEvent.CHANGE, this.InitEncode, false, 1, true);
                        this.iquality.y = this.cy;
                        this.content.addChild(this.iquality);
                        this.content.addChild(this.iformat);
                        this.content.addChild(this.ifolder);
                        U.CallHome("tuyitu_folder_list", {ping:"pong"}, this.FolderCallback);
                        setTimeout(this.FormatChange, 500);
                        break;
                    }
                    case "qqapi":
                    {
                        ltn = new Literal("rm_description");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.idesc = new TextInput(250, 300, U.CleanString(ed.canvas.document.GetName()), 50);
                        this.idesc.t.wordWrap = true;
                        this.idesc.y = this.cy;
                        this.content.addChild(this.idesc);
                        this.cy = this.cy + 65;
                        ltf = new Literal("rm_folder");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.ifolder = new Dropdown(250, 250);
                        this.ifolder.AddItem(U.RM("rm_working"), "", true);
                        this.ifolder.Bind();
                        this.ifolder.y = this.cy;
                        this.ifolder.Disable();
                        this.content.addChild(this.ifolder);
                        this.iformat = new Dropdown(250, 250);
                        this.iformat.AddItem(U.RM("rm_jpeg"), "jpg", true);
                        this.iquality = new Slidebar("rm_quality", 0, 100, 250, 80, false);
                        U.CallHome("qqapi_folder_list", {ping:"pong"}, this.FolderCallback);
                        setTimeout(this.FormatChange, 500);
                        break;
                    }
                    case "sina":
                    {
                        ltn = new Literal("rm_name");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.iname = new TextInput(250, 100, U.CleanString(ed.canvas.document.GetName()));
                        this.iname.t.restrict = "^,\\/&?<>(){}[]#@";
                        this.iname.y = this.cy;
                        this.content.addChild(this.iname);
                        this.cy = this.cy + 35;
                        ltf = new Literal("rm_folder");
                        y = this.cy;
                        x = -2;
                        this.content.addChild();
                        this.cy = this.cy + 18;
                        this.ifolder = new Dropdown(250, 250);
                        this.ifolder.AddItem(U.RM("rm_working"), "", true);
                        this.ifolder.Bind();
                        this.ifolder.y = this.cy;
                        this.ifolder.Disable();
                        this.content.addChild(this.ifolder);
                        this.iformat = new Dropdown(250, 250);
                        this.iformat.AddItem(U.RM("rm_jpeg"), "jpg", true);
                        this.iquality = new Slidebar("rm_quality", 0, 100, 250, 80, false);
                        U.CallHome("sina_folder_list", {ping:"pong"}, this.FolderCallback);
                        setTimeout(this.FormatChange, 500);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        public function FolderCallback(flash.utils:Object) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Object = null;
            this.ifolder.Disable(false);
            this.ifolder.Clear();
            if (flash.utils.success)
            {
                if (flash.utils.payload.length != 0)
                {
                    _loc_2 = 0;
                    while (_loc_2 < flash.utils.payload.length)
                    {
                        
                        _loc_3 = flash.utils.payload[_loc_2];
                        this.ifolder.AddItem(_loc_3.name + " (" + _loc_3.size + ")", _loc_3._id, _loc_2 == 0 ? (true) : (false));
                        _loc_2++;
                    }
                }
                else
                {
                    this.ifolder.AddItem(U.RM("rm_new") + "...", "", true);
                }
                this.ifolder.Bind();
            }
            else
            {
                switch(flash.utils.payload.toLowerCase())
                {
                    case "err:not_logged_in":
                    {
                        new LoginDialog(ed, this.TargetSelect, this.TargetSelect);
                        break;
                    }
                    case "err:no_token":
                    {
                        this.ShowConnect();
                        break;
                    }
                    default:
                    {
                        new AlertDialog(ed, U.RM("rm_error") + "\n" + flash.utils.payload);
                        break;
                    }
                }
            }
            return;
        }// end function

        public function ShowConnect() : void
        {
            this.overlay = new Sprite();
            this.overlay.graphics.beginFill(12961221);
            this.overlay.graphics.drawRect(4, 50, this.width - 8, this.height - 54);
            addChild(this.overlay);
            this.connect = new OpenImageButton(new UrlImageIcon(), "rm_connect_" + this.target.value, "rm_allow_pop_up", 4);
            this.connect.addEventListener(MouseEvent.CLICK, this.ConnectClick, false, 1, true);
            this.connect.x = int(this.width / 2 - this.connect.width / 2);
            this.connect.y = int(this.height / 2 - this.connect.height / 2);
            addChild(this.connect);
            this.overlay.graphics.beginFill(15724527);
            this.overlay.graphics.drawRect(this.connect.x - 5, this.connect.y - 5, this.connect.width + 10, this.connect.height + 10);
            return;
        }// end function

        public function HideConnect() : void
        {
            if (this.overlay != null)
            {
                removeChild(this.overlay);
                removeChild(this.connect);
                this.overlay = null;
                this.connect = null;
            }
            return;
        }// end function

        public function ConnectClick(... args) : void
        {
            U.Connect(this.target.value, this.ConnectCallBack);
            return;
        }// end function

        public function ConnectCallBack(flash.utils:String) : void
        {
            var _loc_2:* = U.JSONResult(flash.utils);
            if (_loc_2.success)
            {
                this.HideConnect();
                this.TargetSelect();
            }
            else
            {
                switch(_loc_2.payload.toLowerCase())
                {
                    case "err:popup_block":
                    {
                        new AlertDialog(ed, "rm_allow_pop_up");
                        break;
                    }
                    case "err:not_logged_in":
                    {
                        new LoginDialog(ed);
                        break;
                    }
                    default:
                    {
                        new AlertDialog(ed, U.RM("rm_error") + "\n" + _loc_2.payload);
                        break;
                    }
                }
            }
            return;
        }// end function

        private function DrawThumb() : void
        {
            var _loc_1:* = ed.canvas.dw;
            var _loc_2:* = ed.canvas.dw;
            if (_loc_1 > 250 || _loc_2 > 250)
            {
                if (ed.canvas.dw / 250 > ed.canvas.dh / 250)
                {
                    _loc_1 = 250;
                    _loc_2 = Math.round(ed.canvas.dh * (_loc_1 / ed.canvas.dw));
                }
                else
                {
                    _loc_2 = 250;
                    _loc_1 = Math.round(ed.canvas.dw * (_loc_2 / ed.canvas.dh));
                }
            }
            this.overview.graphics.clear();
            this.overview.graphics.beginFill(9474192);
            this.overview.graphics.drawRect(0, 0, _loc_1 + 4, _loc_2 + 4);
            this.overview.graphics.beginFill(16777215);
            this.overview.graphics.drawRect(1, 1, _loc_1 + 2, _loc_2 + 2);
            if (this.iformat != null && (this.iformat.value == "png" || this.iformat.value == "tud"))
            {
                this.overview.graphics.beginBitmapFill(new TransparentLarge());
                this.overview.graphics.drawRect(2, 2, _loc_1, _loc_2);
            }
            this.overview.graphics.endFill();
            this.bm.bitmapData = ed.canvas.screenshot;
            this.bm.width = _loc_1;
            this.bm.height = _loc_2;
            this.bm.smoothing = true;
            return;
        }// end function

        private function FormatChange(... args) : void
        {
            this.iquality.visible = this.iformat.value == "jpg";
            this.InitEncode();
            this.DrawThumb();
            return;
        }// end function

        private function InitEncode(... args) : void
        {
            this.ToggleWorking(true);
            setTimeout(this.Encode, 50);
            return;
        }// end function

        private function Encode() : void
        {
            var _loc_1:JPGEncoder = null;
            var _loc_2:PNGEncoder = null;
            var _loc_3:BMPEncoder = null;
            var _loc_4:TUDEncoder = null;
            switch(this.iformat.value)
            {
                case "jpg":
                {
                    _loc_1 = new JPGEncoder(this.iquality.value);
                    ed.canvas.UpdateAfterEdit(-1, 4294967295);
                    this.stream = _loc_1.encode(ed.canvas.screenshot);
                    break;
                }
                case "png":
                {
                    _loc_2 = new PNGEncoder();
                    ed.canvas.UpdateAfterEdit();
                    this.stream = _loc_2.encode(ed.canvas.screenshot);
                    break;
                }
                case "bmp":
                {
                    _loc_3 = new BMPEncoder();
                    ed.canvas.UpdateAfterEdit(-1, 4294967295);
                    this.stream = _loc_3.encode(ed.canvas.screenshot);
                    break;
                }
                case "tud":
                {
                    _loc_4 = new TUDEncoder();
                    ed.canvas.UpdateAfterEdit();
                    this.stream = _loc_4.encode(ed.canvas.document);
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.ToggleWorking(false);
            return;
        }// end function

        private function ToggleWorking(flash.utils:Boolean) : void
        {
            if (flash.utils)
            {
                this.size.text = "";
            }
            else
            {
                this.size.text = U.RM("rm_size") + " " + Math.round(this.stream.length / 1024) + " KB";
            }
            this.message.visible = flash.utils;
            this.ok.visible = !flash.utils;
            this.cancel.visible = !flash.utils;
            return;
        }// end function

        private function Save(... args) : void
        {
            args = new activation;
            var _args:* = args;
            var nm:*;
            var vals:*;
            var url:*;
            var re:* = undefined;
            var host:*;
            var req:*;
            var args:* = ;
            this.ToggleWorking(true);
            switch(this.target.value)
            {
                case "referrer":
                {
                    U.Log("rm_save_to_target");
                    nm = new NetworkManager();
                    vals = new Dictionary();
                    ["type"] = this.iformat.value;
                    ["title"] = this.iname.value;
                    if (U.Param("method", true) != "post")
                    {
                        this.loader = new URLLoader();
                        this.loader.addEventListener(Event.COMPLETE, this.StoreComplete, false, 1, true);
                        this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.StoreError, false, 1, true);
                        this.loader.dataFormat = URLLoaderDataFormat.BINARY;
                        this.loader.load(GenerateRequest("/store/?type=" + this.iformat.value, this.stream, this.iname.value + "." + this.iformat.value, ));
                    }
                    else
                    {
                        url = U.Param("target");
                        nm = new NetworkManager();
                        re = new RegExp("^(?:f|ht)tp(?:s)?://([^/]+)", "im");
                        host = match()[1].toString();
                        Security.loadPolicyFile("http://" +  + "/crossdomain.xml");
                        req = GenerateRequest(, this.stream, this.iname.value, );
                        if (U.IsParam("redirect"))
                        {
                            sendToURL();
                            if (!U.IsParam("redirect"))
                            {
                                navigateToURL(new URLRequest(U.Param("target")), "_self");
                            }
                            if (U.Param("redirect", true) != "false")
                            {
                                navigateToURL(new URLRequest(U.Param("redirect")), "_self");
                            }
                            else
                            {
                                if (this.removecanvas)
                                {
                                    ed.RemoveCanvas();
                                }
                                this.Close();
                            }
                        }
                        else
                        {
                            navigateToURL(, "_self");
                        }
                    }
                    ed.canvas.document.name = this.iname.value + "." + this.iformat.value;
                    ed.canvas.SetTitle(this.iname.value + "." + this.iformat.value);
                    break;
                }
                case "m5cn":
                {
                    U.Log("rm_save_to_m5cn");
                    nm = new NetworkManager();
                    vals = new Dictionary();
                    ["callback"] = "true";
                    ["name"] = this.idesc.value;
                    this.loader = new URLLoader();
                    this.loader.dataFormat = URLLoaderDataFormat.BINARY;
                    this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.StoreError, false, 1, true);
                    this.loader.addEventListener(Event.COMPLETE, function (event:Event)
            {
                Close();
                new M5cnDialog(ed, event.target.data);
                return;
            }// end function
            , false, 1, true);
                    this.loader.load(GenerateRequest("http://api.tuyitu.com/tu/", this.stream, "image." + this.iformat.value, ));
                    break;
                }
                case "tuyitu":
                {
                    U.Log("rm_save_to_tuyitu");
                    nm = new NetworkManager();
                    vals = new Dictionary();
                    ["folder_id"] = this.ifolder.value;
                    ["target"] = this.target.value;
                    this.loader = new URLLoader();
                    this.loader.dataFormat = URLLoaderDataFormat.BINARY;
                    this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.StoreError, false, 1, true);
                    this.loader.addEventListener(Event.COMPLETE, function (event:Event)
            {
                Close();
                return;
            }// end function
            , false, 1, true);
                    this.loader.load(GenerateRequest(ed.context + "/bridge/image/", this.stream, this.iname.value + "." + this.iformat.value, ));
                    ed.canvas.document.name = this.iname.value + "." + this.iformat.value;
                    ed.canvas.SetTitle(this.iname.value + "." + this.iformat.value);
                    break;
                }
                case "qqapi":
                {
                    U.Log("rm_save_to_qqapi");
                    nm = new NetworkManager();
                    vals = new Dictionary();
                    ["folder_id"] = this.ifolder.value;
                    ["target"] = this.target.value;
                    ["message"] = this.idesc.value;
                    this.loader = new URLLoader();
                    this.loader.dataFormat = URLLoaderDataFormat.BINARY;
                    this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.StoreError, false, 1, true);
                    this.loader.addEventListener(Event.COMPLETE, function (event:Event)
            {
                Close();
                return;
            }// end function
            , false, 1, true);
                    this.loader.load(GenerateRequest(ed.context + "/bridge/image/", this.stream, "image.jpg", ));
                    ed.canvas.document.name = this.idesc.value;
                    ed.canvas.SetTitle(this.idesc.value);
                    break;
                }
                case "sina":
                {
                    U.Log("rm_save_to_sina");
                    nm = new NetworkManager();
                    vals = new Dictionary();
                    ["folder_id"] = this.ifolder.value;
                    ["target"] = this.target.value;
                    ["title"] = this.iname.value;
                    this.loader = new URLLoader();
                    this.loader.dataFormat = URLLoaderDataFormat.BINARY;
                    this.loader.addEventListener(IOErrorEvent.IO_ERROR, this.StoreError, false, 1, true);
                    this.loader.addEventListener(Event.COMPLETE, function (event:Event)
            {
                Close();
                return;
            }// end function
            , false, 1, true);
                    this.loader.load(GenerateRequest(ed.context + "/bridge/image/", this.stream, "image.jpg", ));
                    ed.canvas.document.name = this.iname.value;
                    ed.canvas.SetTitle(this.iname.value);
                    break;
                }
                default:
                {
                    U.Log("rm_save_to_computer");
                    this.file = new FileReference();
                    this.file.addEventListener(IOErrorEvent.IO_ERROR, this.SaveError, false, 1, true);
                    this.file.addEventListener(Event.CANCEL, this.SaveCancel);
                    this.file.addEventListener(Event.COMPLETE, this.SaveComplete);
                    this.file.save(this.stream, this.iname.value + "." + this.iformat.value);
                    break;
                }
            }
            return;
        }// end function

        private function SaveCancel(event:Event) : void
        {
            this.ToggleWorking(false);
            return;
        }// end function

        private function SaveComplete(event:Event) : void
        {
            ed.canvas.document.name = this.iname.value + "." + this.iformat.value;
            ed.canvas.SetTitle(this.iname.value + "." + this.iformat.value);
            this.Close();
            return;
        }// end function

        private function SaveError(event:Event) : void
        {
            U.Log("rm_save_error");
            var _loc_2:* = new URLRequestHeader("Content-type", "multipart/form-data");
            var _loc_3:* = new URLRequest("/pop/?name=" + this.iname.text + "." + this.iformat.value + "&type=" + this.iformat.value);
            _loc_3.requestHeaders.push(_loc_2);
            _loc_3.method = URLRequestMethod.POST;
            _loc_3.data = this.stream;
            navigateToURL(_loc_3, "_self");
            return;
        }// end function

        private function StoreComplete(event:Event) : void
        {
            var _loc_2:* = undefined;
            var _loc_3:String = null;
            if (event.target.data != "")
            {
                _loc_2 = String(event.target.data);
                if (_loc_2.indexOf("ERR") != -1)
                {
                    this.StoreError(null);
                    this.Close();
                }
                else
                {
                    _loc_3 = U.Param("target");
                    if (_loc_3.indexOf("?") == -1)
                    {
                        _loc_3 = _loc_3 + "?";
                    }
                    else
                    {
                        _loc_3 = _loc_3 + "&";
                    }
                    _loc_3 = _loc_3 + ("image=" + encodeURI("http://api.tuyitu.com/_temp/" + _loc_2));
                    _loc_3 = _loc_3 + ("&type=" + encodeURI(this.iformat.value));
                    _loc_3 = _loc_3 + ("&title=" + encodeURI(this.iname.text));
                    if (U.IsParam("redirect"))
                    {
                        sendToURL(new URLRequest(_loc_3));
                        if (U.Param("redirect", true) != "false")
                        {
                            navigateToURL(new URLRequest(U.Param("redirect")), "_self");
                        }
                        else
                        {
                            this.Close();
                        }
                    }
                    else
                    {
                        navigateToURL(new URLRequest(_loc_3), "_self");
                    }
                }
            }
            return;
        }// end function

        private function StoreError(event:Event) : void
        {
            this.Close();
            new AlertDialog(ed, "rm_error");
            return;
        }// end function

        public function Close(... args) : void
        {
            if (ed != null)
            {
                if (this.removecanvas)
                {
                    ed.RemoveCanvas();
                }
                ed.removeChild(this);
                ed.RemoveModal();
                ed.CheckToShowSplash();
            }
            return;
        }// end function

    }
}
